package springinpractice.ch8.mailinglist.dao;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import springinpractice.ch8.mailinglist.model.Subscriber;
import springinpractice.common.dao.AbstractHibernateDao;

/**
 * @author Willie Wheeler and John Wheeler
 * Book : 'Spring In Practice' [Book written by Willie Wheeler and John Wheeler]
 **/

@Repository("subscriberDao")
public class HbnSubscriberDao extends AbstractHibernateDao<Subscriber>
	implements SubscriberDao {
	
	private static final Logger log = Logger.getLogger(HbnSubscriberDao.class);
	
	public void disableAllByEmail(String email) {
		Assert.notNull(email, "email required");
		Query q = getSession().createQuery(
			"update Subscriber set enabled = false where email = :email");
		q.setString("email", email);
		int rowCount = q.executeUpdate();
		log.info("Disabled " + rowCount + " subscriptions");
	}
}
